﻿using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Jungle.Code.DAL;
using Jungle.Code.BLL;
using Jungle.Code.Core;
using Jungle.Code.Utils;

using Jungle.Prodamo.AdvertManager;
using Jungle.Prodamo.Core;
using Jungle.Prodamo.MostNeedFunctions;
using Jungle.Prodamo.PriceManager;

public partial class User : BaseUserModule
{
    string query;
    string t;
    protected override void Render(HtmlTextWriter writer)
    {
        if (!IsPostBack)
        {
            query = Request.QueryString["query"].ToString().ToLower();
            if (Request["t"] != null)
            {
                t = Request.QueryString["t"].ToString().ToLower();
                switch (t)
                {
                    case "2":
                        ByPrice(writer);
                        break;
                    case "3":
                        GetCompnayes(writer);
                        break;
                    case "1":
                        ByAdvert(writer);
                        break;
                }
            }
            
        }
        base.Render(writer);
    }
    protected HtmlTextWriter GetCompnayes(HtmlTextWriter writer)
    {
        UserManager firms = new UserManager();
        firms.UsertType = false;
        firms.GetByType();

        writer.AddAttribute(HtmlTextWriterAttribute.Class, "advm_table");
        writer.RenderBeginTag(HtmlTextWriterTag.Table);
        writer.RenderBeginTag(HtmlTextWriterTag.Tr);
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "advm_td");
        writer.RenderBeginTag(HtmlTextWriterTag.Td);
        writer.Write("Результаты поиска");
        writer.RenderEndTag();
        writer.RenderEndTag();
        bool c = false;
        if (query.Length > 1)
        {
            foreach (UserManager f in firms.UserList)
            {
                if (f.Edrpou == query) Response.Redirect("~/ru/userinfo.aspx?uId=" + f.UserId.ToString());

                if (f.Organization.ToLower().IndexOf(query.ToLower()) != -1)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<a href=\"" + UrlHelper.GetSiteUrl() + "/ru/userinfo.aspx?uId=" + f.UserId.ToString() + "\">" + f.Organization + "</a>");
                    writer.RenderEndTag();
                    writer.RenderEndTag();
                    c = true;
                }
            }
        }
        if(!c)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Поиск не дал результатов");
            writer.RenderEndTag();
            writer.RenderEndTag();
        }
        writer.RenderEndTag();
        return writer;
    }
    protected HtmlTextWriter ByAdvert(HtmlTextWriter writer)
    {

        writer.AddAttribute(HtmlTextWriterAttribute.Class, "advm_table");
        writer.RenderBeginTag(HtmlTextWriterTag.Table);
        writer.RenderBeginTag(HtmlTextWriterTag.Tr);
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "advm_td");
        writer.RenderBeginTag(HtmlTextWriterTag.Td);
        writer.Write("Результаты поиска");
        writer.RenderEndTag();
        writer.RenderEndTag();

        bool c = false;
        if (query.Length > 1)
        {
            // Поиск по объявлениям
            Advert advs = new Advert();
            advs.GetAll();
            foreach (Advert adv in advs.AdvertList)
            {
                if (adv.Title.ToLower().IndexOf(query.ToLower()) != -1
                    ||
                    adv.Body.ToLower().IndexOf(query.ToLower()) != -1
                    )
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<a href=\"" + UrlHelper.GetSiteUrl() + "/ru/adverts.aspx?cmd=Show&Id=" + adv.AdvertId.ToString() + "\">" + adv.Title + "</a>");
                    writer.RenderEndTag();
                    writer.RenderEndTag();
                    c = true;
                }
            }
            // Конец поиска по обяъвлениям
        }
        if(!c)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("Поиск не дал результатов");
            writer.RenderEndTag();
            writer.RenderEndTag();
        }
        writer.RenderEndTag();
        return writer;
    }
    protected HtmlTextWriter ByPrice(HtmlTextWriter writer)
    {

        writer.AddAttribute(HtmlTextWriterAttribute.Class, "advm_table");
        writer.RenderBeginTag(HtmlTextWriterTag.Table);
        writer.RenderBeginTag(HtmlTextWriterTag.Tr);
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "advm_td");
        writer.RenderBeginTag(HtmlTextWriterTag.Td);
        writer.Write("Результаты поиска");
        writer.RenderEndTag();
        writer.RenderEndTag();

        using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
        {
            string sql = "select * from dbo.Price where [Description] like '%" + query + "%'";
            SqlCommand command = new SqlCommand(sql, connect);
            SqlDataReader reader = null;

            command.Connection.Open();
            reader = command.ExecuteReader();

            bool c = false;
            if (query.Length > 1)
            {
                while (reader.Read())
                {
                    Price p = new Price();
                    p.PriceId = (int)reader[0];
                    p.GetById();

                    Model m = new Model();
                    m.ModelId = p.ModelId;
                    m.GetById();

                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("<a href=\"" + UrlHelper.GetSiteUrl() + "/ru/price.aspx?pId=" + p.PriceId.ToString() + "\">" + m.Title + "</a>");
                    writer.RenderEndTag();
                    writer.RenderEndTag();
                    c = true;
                }
            }

            if(!c)
            { 
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write("Поиск не дал результатов");
                writer.RenderEndTag();
                writer.RenderEndTag();
            }               
            command.Connection.Close();
        }

        writer.RenderEndTag();
        return writer;
    }
}
